package com.ruoyi.common.core.redis;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.github.benmanes.caffeine.cache.Cache;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * spring redis 工具类
 *
 * @author ruoyi
 **/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
//    @Autowired
//    public RedisTemplate redisTemplate;

    @Autowired
    private Cache<String, Object> caffeineCache;


    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key   缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value) {
        caffeineCache.put(key, value);
//        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key      缓存的键值
     * @param value    缓存的值
     * @param timeout  时间
     * @param timeUnit 时间颗粒度
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
//        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
        caffeineCache.put(key, value);
    }


    /**
     * 判断 key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public Boolean hasKey(String key) {
//        return redisTemplate.hasKey(key);
        return caffeineCache.asMap().containsKey(key);
    }

    /**
     * 获得缓存的基本对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public <T> T getCacheObject(final String key) {
//        ValueOperations<String, T> operation = redisTemplate.opsForValue();
//        return operation.get(key);
        Object ifPresent = caffeineCache.getIfPresent(key);
        if (ifPresent instanceof ArrayList) {
            String json = JSON.toJSONString(ifPresent);
            return (T) JSONArray.parse(json);
        }
        return (T) ifPresent;
    }

    /**
     * 删除单个对象
     *
     * @param key
     */
    public boolean deleteObject(final String key) {
//        return redisTemplate.delete(key);
        caffeineCache.invalidate(key);
        return true;
    }

    /**
     * 删除集合对象
     *
     * @param collection 多个对象
     * @return
     */
    public boolean deleteObject(final Collection collection) {
//        return redisTemplate.delete(collection) > 0;
        caffeineCache.invalidateAll(collection);
        return true;
    }


    /**
     * 获得缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     * @return 对象列表
     */
    public Collection<String> keys(final String pattern) {
//        return redisTemplate.keys(pattern);
        ConcurrentMap<@NonNull String, @NonNull Object> stringObjectConcurrentMap = caffeineCache.asMap();
        Set<@NonNull String> strings = stringObjectConcurrentMap.keySet();

        String key = pattern.replace("*", "");
        Collection<String> collection = new ArrayList<>();
        strings.forEach(r -> {
            if (r.contains(key)) {
                collection.add(r);
            }
        });
        return collection;
    }
}
